(function(angular) {
	'use strict';
	angular.module("fs.waf").factory("patchTemplateService", patchTemplateService);
	patchTemplateService.$inject = [ '$q', 'loggerFactory', 'ajaxServiceFactory' ];
	function patchTemplateService($q, loggerFactory, ajaxServiceFactory) {
		var log = loggerFactory.getLogger("patchTemplateService");
		var ajaxService = ajaxServiceFactory.buildAjaxService({
			getYesNoOptions : {
				url : 'do/cda/dataList/yesno',
				method : 'get'
			},
			getServiceOptions : {
				url : 'do/cda/dataList/serviceCode',
				method : 'get'
			}
		});
		
		function getServiceOptions() {
			var defer = $q.defer();
			ajaxService.getService().then(function(response) {
				defer.resolve(response);
			});
			return defer.promise;
		}
		function getYesNoOptions() {
			var defer = $q.defer();
			ajaxService.getYesNoOptions().then(function(response) {
				defer.resolve(response ? response : {});
			});
			return defer.promise;
		};
		
		function initOptions(ctrl){
			var defer1 = $q.defer();
			var defer2 = $q.defer();
			ajaxService.getYesNoOptions().then(function(response) {
				ctrl.userGlobalValueOptions = response;
				defer1.resolve();
			}, angular.noop);
			ajaxService.getServiceOptions().then(function(response) {
				ctrl.serviceCodeOptions = response;
				defer2.resolve();
			});
			
			return $q.all({
				defer1 : defer1.promise,
				defer2 : defer2.promise
			});
		};
		
		function initOptionForSearch(ctrl){
			var defer1 = $q.defer();
			ajaxService.getServiceOptions().then(function(response) {
				if (response && response.data)
				defer1.resolve(response);
			}, angular.noop);
			return $q.all({
				serviceOptions : defer1.promise,
			});
		};
		
		return {
			initOptionForSearch : initOptionForSearch,
			initOptions : initOptions
		};
	}
})(angular);